package com.example.widgetcase.slice;

import com.example.widget_lib.bean.CircleBean;
import com.example.widget_lib.utils.ToastUtils;
import com.example.widget_lib.widget.ring.RingView;
import com.example.widgetcase.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.utils.Color;

import java.text.DecimalFormat;
import java.util.ArrayList;

public class RingViewAbilitySlice extends BaseSlice {

    private DecimalFormat percentFormat = new DecimalFormat("#.##%");
    private RingView ringv;
    private RingView ringv1;
    private RingView ringv2;
    private RingView ringv3;

    @Override
    public int getChildUIContent() {
        return ResourceTable.Layout_ring_view;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        ringv = (RingView) findComponentById(ResourceTable.Id_ring_view);
        ringv1 = (RingView) findComponentById(ResourceTable.Id_ring_view1);
        ringv2 = (RingView) findComponentById(ResourceTable.Id_ring_view2);
        ringv3 = (RingView) findComponentById(ResourceTable.Id_ring_view3);
        Button button = (Button) findComponentById(ResourceTable.Id_button);
        Button button1 = (Button) findComponentById(ResourceTable.Id_button1);
        button1.setClickedListener(component -> {
            ToastUtils.show(getApplicationContext(),"刷新一下");
            ringv1.setCenterTxtColor("迪迦", Color.getIntColor("#776948"));
            showRingView();
        });
        button.setClickedListener(component -> {
            present(new LegendAbilitySlice(), new Intent().setParam("name", "HorProBar"));
        });

        initRingView();
    }

    private void initRingView() {
        int valueOne = 12;
        int valueTwo = 24;
        ringv.setCenterTxt("测试", "看看");
//        LogUtil.loge("============"+progress);
        ringv.setData(35, 2, "第一个50",
                "第二个50");

        showRingView();
        showRingView2();
        showRingView3();
    }

    private void showRingView() {
        int valueSum1 = 100;
        ArrayList<CircleBean> mList1 = new ArrayList<CircleBean>();
        CircleBean circleBean;
        int rvMax1 = ringv1.getMax();
        float startPro1 = 0f;
        int size1 = 10;
        for (int i = 0; i < size1; i++) {
            circleBean = new CircleBean();
            circleBean.setStartPro(startPro1);
            circleBean.setEndPro((10f / valueSum1) * rvMax1);
            circleBean.setCenterPro(startPro1 + circleBean.getEndPro() / 2);
            circleBean.setDesc(percentFormat.format(0.10f));
            startPro1 += circleBean.getEndPro();
            mList1.add(circleBean);
        }
//        ringv1.setImagePic(com.example.widget_lib.ResourceTable.Media_hb);
        ringv1.setData(mList1, 2, 3);
    }

    private void showRingView2() {
        int valueSum = 60;
        ArrayList<CircleBean> mList = new ArrayList<CircleBean>();
        CircleBean cb;
        int rvMax = ringv2.getMax();
        float startPro = 0f;
        int size = 3;
        for (int i = 0; i < size; i++) { // 计算数值占圆环的比例
            cb = new CircleBean();
            cb.setStartPro(startPro);

            switch (i) {
                case 0:
                    cb.setEndPro((12f / valueSum) * rvMax);
                    break;
                case 1:
                    cb.setEndPro((32f / valueSum) * rvMax);
                    break;
                default:
                    cb.setEndPro((16f / valueSum) * rvMax);
            }

            cb.setCenterPro(startPro + cb.getEndPro() / 2);
            cb.setDesc("di" + i + "ge");
            startPro += cb.getEndPro();
//            Common.log_d("201811091554", cb.toString())
            mList.add(cb);
        }
        ringv2.setData(mList, 1, 1);
        ringv2.setOuterIsEnable(true);
        ringv2.setListener(new RingView.OuterListener() {
            @Override
            public void onOuterClick(String label) {
                ringv2.setCenterTxt("凹凸曼");
                showRingView2();
            }
        });
    }

    private void showRingView3() {
        int valueSum = 60;
        ArrayList<CircleBean> mList = new ArrayList<CircleBean>();
        CircleBean cb;
        int rvMax = ringv3.getMax();
        float startPro = 0f;
        int size = 3;
        for (int i = 0; i < size; i++) { // 计算数值占圆环的比例
            cb = new CircleBean();
            cb.setStartPro(startPro);

            switch (i) {
                case 0:
                    cb.setEndPro((12f / valueSum) * rvMax);
                    break;
                case 1:
                    cb.setEndPro((32f / valueSum) * rvMax);
                    break;
                default:
                    cb.setEndPro((16f / valueSum) * rvMax);
            }

            cb.setCenterPro(startPro + cb.getEndPro() / 2);
//            cb.setDesc("di" + i + "ge");
            startPro += cb.getEndPro();
//            Common.log_d("201811091554", cb.toString())
            mList.add(cb);
        }
        ringv3.setCenterTxt("20~29岁", "男性");
        ringv3.setData(mList, 1, 2);
    }
}
